using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Services.Menu
{
    
    public enum TextPos
    {
        Center,
        Right,
        Left
    }
    public class MyButton
    {
        public static bool ALLOW_VIBRATE = false;
        public String text;
        public SpriteFont font;
        public Vector2 position;
        public Color textColor;
        public Color textureColor;
        public Texture2D texture;
        public Texture2D bgTexture;
        public Rectangle rectangle;
        public bool enabled;
        public float textScale;

        public TextPos textPos;
        public float textRotation;
        private float prevTextRotation;
        private Vector2 rotationCenter;
        public Color bgTextureColor;

        public String text2;
        public Color textColor2;
        public SpriteFont font2;
        public TextPos textPos2;
        public Vector2 position2;
        public float textScale2;
        public float alpha = 1f;
        public bool adjustVerticalAlignement = false;
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font, TextPos textPos, Texture2D texture, Color textureColor)
        {

            this.textColor = textColor;
            this.font = font;
            this.text = text;
            this.rectangle = rectangle;
            this.texture = texture;
            this.textureColor = textureColor;
            this.enabled = true;
            textScale = 1;
            textScale = 2;
            this.textPos = textPos;
            UpdateTextPos();
            prevTextRotation = textRotation = 0;
            rotationCenter = Vector2.Zero;
        }
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font, TextPos textPos,
            String text2, Color textColor2, SpriteFont font2, TextPos textPos2, Texture2D texture, Color textureColor)
        {

            this.textColor = textColor;
            this.font = font;
            this.text = text;
            this.rectangle = rectangle;
            this.texture = texture;
            this.textureColor = textureColor;
            this.enabled = true;
            this.textPos = textPos;

            this.text2 = text2;
            this.font2 = font2;
            this.textPos2 = textPos2;
            this.textColor2 = textColor2;

            textScale = 1;
            textScale = 2;

            UpdateTextPos();
            prevTextRotation = textRotation = 0;
            rotationCenter = Vector2.Zero;


        }
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font)
            : this(rectangle, text, textColor, font, TextPos.Center, null, Color.Transparent)
        { }
        public MyButton(Rectangle rectangle, String text, Color textColor, SpriteFont font, TextPos textPos)
            : this(rectangle, text, textColor, font, textPos, null, Color.Transparent)
        {

        }
        public MyButton(Rectangle rectangle, Texture2D texture, Color textureColor)
            : this(rectangle, "", Color.Transparent, null, TextPos.Center, texture, textureColor)
        { }
        public MyButton(Rectangle rectangle, Texture2D texture, Color textureColor, Texture2D bgTexture, Color bgTextureColor)
            : this(rectangle, "", Color.Transparent, null, TextPos.Center, texture, textureColor)
        {
            this.bgTexture = bgTexture;
            this.bgTextureColor = bgTextureColor;
        }
        public void Draw(SpriteBatch batch)
        {
            if (!enabled)
                return;
            //position = OptionsMenu.centerString(rectangle, text, font, textScale);
            if (bgTexture != null)
                batch.Draw(bgTexture, rectangle, bgTextureColor * alpha);
            if (texture != null)
                batch.Draw(texture, rectangle, textureColor * alpha);
            if (font != null)
            {
                if (textRotation != prevTextRotation)
                {
                    prevTextRotation = textRotation;
                    rotationCenter = font.MeasureString(text) / 2;
                }
                if (textPos == TextPos.Center)
                    centerString();
                if (textRotation == 0)
                    batch.DrawString(font, text, position, textColor);
                else
                    batch.DrawString(font, text, position, textColor, textRotation, font.MeasureString(text) / 2, textScale, SpriteEffects.None, 1);
            }
            if (font2 != null)
            {
                if (textPos2 == TextPos.Center)
                    centerString();
                else
                    UpdateTextPos();
                batch.DrawString(font2, text2, position2, textColor2);

            }
        }
        public bool tapped_on(Point p)
        {
            var tapped = enabled && rectangle.Contains(p);
            //if (tapped && bgTexture != null && texture != null)
            //    alpha = 0.5f;
            if (tapped && ALLOW_VIBRATE)
                Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 50));
            return tapped;
        }
        public bool tapped_on(Vector2 v)
        {
            var tapped = enabled && rectangle.X < v.X && rectangle.Y < v.Y && rectangle.X + rectangle.Width > v.X && rectangle.Y + rectangle.Height > v.Y;
            //if (tapped && bgTexture != null && texture != null)
            //    alpha = 0.5f;
            if (tapped && ALLOW_VIBRATE)
                Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 50));
            return tapped;
        }

        public bool tapped_on(Vector2 v, bool vibrate)
        {
            var tapped = enabled && rectangle.X < v.X && rectangle.Y < v.Y && rectangle.X + rectangle.Width > v.X && rectangle.Y + rectangle.Height > v.Y;
            //if (tapped && bgTexture != null && texture != null)
            //    alpha = 0.5f;
            if (tapped && vibrate && ALLOW_VIBRATE)
                Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 50));
            return tapped;
        }
        public bool tapped_on(Point p, bool vibrate)
        {
            var tapped = enabled && rectangle.Contains(p);
            //if (tapped && bgTexture != null && texture != null)
            //    alpha = 0.5f;
            if (tapped && vibrate && ALLOW_VIBRATE)
                Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 50));
            return tapped;
        }

        public Vector2 centerString(Rectangle rectangle, string text, SpriteFont spriteFont)
        {
            Vector2 destinazione = new Vector2();
            if (font == null || text == null || text == "") return new Vector2(rectangle.Center.X, rectangle.Center.Y);
            Vector2 dimStringa = spriteFont.MeasureString(text);

            destinazione.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            destinazione.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

            return destinazione;
        }
        public Vector2 centerString(Rectangle rectangle, string text, SpriteFont spriteFont, float scale)
        {
            Vector2 destinazione = new Vector2();
            if (font == null || text == null || text == "") return new Vector2(rectangle.Center.X, rectangle.Center.Y);
            Vector2 dimStringa = spriteFont.MeasureString(text) * scale;

            destinazione.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            destinazione.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

            return destinazione;
        }
        public void centerString()
        {

            if (font == null || text == null || text == "")
            {
                return;
            }
            Vector2 dimStringa = font.MeasureString(text);

            position.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            position.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;

            if (font2 == null || text2 == null || text2 == "")
            {
                return;
            }
            dimStringa = font.MeasureString(text2);

            position2.X = rectangle.X + rectangle.Width / 2 - dimStringa.X / 2;
            position2.Y = rectangle.Y + rectangle.Height / 2 - dimStringa.Y / 2;
            position2.Y = rectangle.Y + rectangle.Height - font.MeasureString(text2).Y;
        }
        public void UpdateTextPos()
        {
            if (textPos == TextPos.Center)
            {
                position = centerString(rectangle, text, font, textScale);
            }
            else if (textPos == TextPos.Left)
            {
                position = centerString(rectangle, text, font, textScale);
                position.X = rectangle.X + 5;
                if (adjustVerticalAlignement)
                {
                    if(text2==null)
                        position.Y += (int)(font.MeasureString(text).Y / 2);
                    else
                        position.Y += (int)(font.MeasureString(text).Y / 5);
                    //position2.Y += font2.MeasureString(text2).Y / 2;
                }
            }
            else
                if (textPos == TextPos.Right)
                {
                    position = centerString(rectangle, text, font, textScale);
                    position.X = rectangle.X + rectangle.Width - 5 - font.MeasureString(text).Length();
                }

            if (text2 != null && font2 != null)
            {
                if (textPos2 == TextPos.Center)
                {
                    position2 = centerString(rectangle, text2, font2, textScale2);

                }
                else if (textPos2 == TextPos.Left)
                {
                    position2 = centerString(rectangle, text2, font2, textScale2);
                    position2.X = rectangle.X + 5;
                    if (adjustVerticalAlignement)
                    {
                        //position.Y += font.MeasureString(text).Y / 2;
                        //position2.Y += rectangle.Y + rectangle.Height;// -font2.MeasureString(text2).Y / 5;//(int)(font2.MeasureString(text2).Y / 0.1);
                        position2.Y = rectangle.Y + rectangle.Height - font.MeasureString(text2).Y/12*10;
                        return;
                    }
                }
                else
                    if (textPos2 == TextPos.Right)
                    {
                        position2 = centerString(rectangle, text2, font2, textScale2);
                        position2.X = rectangle.X + rectangle.Width - 5 - font.MeasureString(text2).Length();
                    }
                position2.Y = rectangle.Y + rectangle.Height - font.MeasureString(text2).Y;
            }
        }
    }
}
